<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('供应商资料共享')" />
	<style type="text/css">
		.layui-elem-quote{padding:5px;}
		hr{margin-bottom:0}
	</style>
</head>
<body>
<form class="layui-form" id="form-customer-add">
	<blockquote class="layui-elem-quote">
		<input type="radio" name="bShared" value="1" title="完全共享" lay-filter="bSharedRadio" checked>
		<hr/>
	</blockquote>
	<blockquote class="layui-elem-quote">
		<input type="radio" name="bShared" value="0" title="个别共享<span class='f_red'>（不填写数据默认不共享）</span>" lay-filter="bSharedRadio" th:checked="!${providerInfo?.bShared}">
		<hr/>
		<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
	</blockquote>
</form>
<th:block th:include="include :: footer" />
<script type="text/html" id="toolbar">
<div class="layui-btn-container">
	<div class="layui-btn layui-btn-normal layui-table-tools" lay-event="addBtn">
		<i class="dhicon dh-plus"></i> 新增
	</div>
</div>
</script>
<script type="text/html" id="barDh">
    <a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" lay-event="del">&nbsp;删除</a>	         
</script>
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "crm/provider";
	/*]]>*/
</script>
<script type="text/javascript">
var $,layer,form,table,listData;
layui.use(['table','form','layer','jquery'], function () {
    $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table;
  	$.ajax({
		url: prefix + '/sharedlist?gprovide='+window.location.href.split("=")[1],
        type: "get",
        dataType: "json",
        success: function(result) {
        	listData=result.data;
        	$.tablelist.init({
                elem: '#dhtable'
                ,data: listData
                ,page:false
                ,limit:10000
                ,cols: [[
                	{field: 'gprovide',title: '客户编号',hide:true},
        	        {field: 'pUser',title: '采购员编号'},
        	       	{field: 'pUserName',title: '采购员'},
        	       	{field: 'pUserDeptId',title: '部门编号',hide:true},
        	       	{field: 'pUserDeptName',title: '部门'},
        	       	{title: '操作',templet:'#barDh'}
                	]
                ]
        		,toolbar: '#toolbar'
				,height:'full-110'
				,done:function(res, curr, count){
					listData = table.cache.dhtable;//每次重载都赋值更新表缓存
				    if($("[name='bShared']:checked").val()==1)$("[lay-id='dhtable']").hide();
				}
            });
        }
  	});
  	//监听工具条
	table.on('tool(ftable)', function (obj) {
		var data = obj.data, event = obj.event, tr = obj.tr,vIndex;//此处的data就是表格里面的值
        vIndex=$("tr").index(obj.tr)-1;
		switch(event){
			case "del":
				obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
				listData=table.cache.dhtable;
				for(var i=0; i < listData.length; i++)if(!listData[i] || listData[i].length==0)listData.splice(i, 1);//删除一项
				table.reload('dhtable',{data : listData});
				break;
		}
	});
	//头部event事件
	table.on('toolbar(ftable)', function(obj){
		var checkStatus = table.checkStatus('dhtable');
	    switch(obj.event){
	    	case 'addBtn':
	    		$.modal.open('选择员工-采购员','/system/user/userselect?pagetype=checkbox','925','465',function(index){
					var res = window["layui-layer-iframe" + index].callbackdata();
			        if(res.length > 0){
			        	console.log(res);
			        	$.each(res,function(i,v){
			        		var flag=true;
			        		$.each(listData,function(i2,v2){
			        			if(v.userId==v2.pUser)flag=false;
			        		})
			        		if(flag)listData.push({"gprovide":window.location.href.split("=")[1],"pUser":v.userId,"pUserName":v.userName,"pUserDeptId":v.deptPath,"pUserDeptName":v.deptName})
		        		});
			        	table.reload('dhtable',{data : listData});
			       	 	layer.closeAll();
			        }else{
			            layer.msg("请选择人员！", {icon:7});
			        }
			    });
	    	break;
	    };
	});
	//共享设置单选框事件
  	form.on('radio(bSharedRadio)', function(data){
  		if(data.value==1)$("[lay-id='dhtable']").hide();
  		else $("[lay-id='dhtable']").show();
	}); 
 });
//提交调用的方法
function submitHandler() {
	if($("input[name='bShared']:checked").val()==1){
		$.operate.save(prefix + "/deleteshared", {'gprovide':window.location.href.split("=")[1]},function(res){
			if(res.code==0)$.operate.save(prefix + "/editpost", {'providerInfoItems':JSON.stringify([{'gprovide':window.location.href.split("=")[1],'bShared':1}])});
		});
	}else{
		$.operate.save(prefix + "/deleteshared", {'gprovide':window.location.href.split("=")[1]});
		if(table.cache.dhtable.length)$.operate.save(prefix + "/addsharedpost", {'providerSharedItems':JSON.stringify(table.cache.dhtable, null, 2)});
		$.operate.save(prefix + "/editpost", {'providerInfoItems':JSON.stringify([{'gprovide':window.location.href.split("=")[1],'bShared':0}])});
	}
}
</script>
</body>
</html>